package com.dataseek.iot.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.device.entity.AlarmLog;
import com.dataseek.iot.device.mapper.AlarmLogMapper;
import com.dataseek.iot.device.service.AlarmLogService;
import com.dataseek.iot.device.vo.AlarmLogVo;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class AlarmLogServiceImpl extends ServiceImpl<AlarmLogMapper, AlarmLog> implements AlarmLogService {


    @Autowired
    private AlarmLogMapper alarmLogMapper;

    @Autowired
    private IDServiceHelper idServiceHelper;

    /*
    分页查询
     */
    @Override
    public AlarmLogVo queryAction(String deviceCode, String alarmType, String jarCode, String isDeal,String startTime,String endTime, Integer num, Integer size) {
        AlarmLogVo alarmLogVo = new AlarmLogVo();
        QueryWrapper<AlarmLog> qw = new QueryWrapper<>();
        if (deviceCode != null && !deviceCode.isEmpty()) {
            qw.like("device_code", deviceCode);
        }
        if (alarmType != null && !alarmType.isEmpty()) {
            qw.eq("alarm_type", Integer.parseInt(alarmType));
        }

        if (jarCode != null && !jarCode.isEmpty()) {
            qw.eq("jar_code", jarCode);
        }
        //处理类型
        if (isDeal != null && !isDeal.isEmpty()) {
            qw.eq("isdeal", Integer.parseInt(isDeal));
        }else {//默认查询未处理状态日志
            qw.eq("isdeal", 0);
        }
        //查询日期
        if(startTime!=null&&!startTime.isEmpty()){
            if(endTime!=null&&!endTime.isEmpty()){
                qw.between("alarm_time",startTime,endTime);
            }
        }
        qw.orderByDesc("alarm_time");
        IPage<AlarmLog> page = new Page<>(num, size);
        List<AlarmLog> mapList = alarmLogMapper.selectPage(page, qw).getRecords();
        alarmLogVo.setPage(num);
        alarmLogVo.setSize(size);
        alarmLogVo.setTotal(page.getTotal());
        alarmLogVo.setList(mapList);
        return alarmLogVo;
    }

    @Override
    public Integer insertAction(AlarmLog alarmLog) {
        //根据deviceCode查询factory_code
        Map infoMap=alarmLogMapper.queryByDeviceCodeMap(alarmLog.getDeviceCode());
        String line_code= infoMap.get("line_code").toString();
        String factory_code= infoMap.get("factory_code").toString();
       String jar_code= infoMap.get("jar_code").toString();
        long alarmId=idServiceHelper.nextId();
        alarmLog.setAlarmId(alarmId);
        alarmLog.setWorkLineCode(line_code);
        alarmLog.setFactoryCode(factory_code);
        alarmLog.setJarCode(jar_code);
        Integer result=alarmLogMapper.insert(alarmLog);
        return result;
    }

    @Override
    public Integer updateAction(AlarmLog alarmLog) {
        Integer result=alarmLogMapper.updateById(alarmLog);
        return result;
    }

    /**
     * 更新日志状态
     * @param AlarmId
     * @return
     */
    @Override
    public Integer updateStatus(long AlarmId) {
        UpdateWrapper<AlarmLog> uw=new UpdateWrapper<>();
        uw.eq("alarmid",AlarmId).set("isdeal",1);
        int result= alarmLogMapper.update(null,uw);
        return result;
    }
}
